<template>
    <view class="container" :style="themeColor">
        <view class="search-bar-box">
            <search-bar-city
                ref="searchBarCity"
                placeholderText="输入地址为你精选门店"
                @searchFn="searchFn"
                :currentCity="selectCity"
                :param="param"
                :location="location"
                @upFn="upFn"></search-bar-city>
        </view>

        <view class="current-city main">
            <mescroll-uni
                v-if="loadOk"
                ref="mescrollRef"
                @init="mescrollInit"
                :height="listHeight"
                :down="downOption"
                @down="downRefresh"
                :up="upOption"
                @up="getData">
                <view class="current-lbs">
                    <text class="title">当前定位</text>
                    <text class="lbs-name">{{ currentCity ? currentCity : "定位中" }}</text>
                </view>
                <shop-item
                    v-for="(item, index) in shopDatas"
                    :key="index"
                    :item="item"
                    @selectFn="selectFn"></shop-item>
            </mescroll-uni>
        </view>
		<privacy-agreement></privacy-agreement>
    </view>
</template>

<script>
import { GePurchaseBusinessList } from "@/service/api/purchase.js"
import searchBarCity from "@/components/common/search-bar-city.vue"
import shopItem from "@/components/login/shop-item.vue"
import { getElementHeight } from "@/uni-sub-base/utils/elementUtil.js"
import { getLocation, getBatchSmartParse } from "@/uni-sub-base/utils/mapUtil.js"
import { batchSmartParse } from "@/service/api/member.js"
import privacyAgreement from "@/components/privacy-agreement/privacy-agreement.vue"
import authorizeUtils from "@/utils/authorizeUtils.js"
export default {
    components: {
        searchBarCity,
        shopItem,
		privacyAgreement
    },
    data() {
        return {
            shopDatas: [],
            listHeight: "500px",
            loadOk: false,
            lbs: null,
            param: {
                cityId: 0,
                address: "",
                lng: 0,
                lat: 0,
                searchKey: "",
            },
            location: {
                AddressName: "",
                ContactName: "",
                ContactTel: "",
                ContactAddress: "",
                IsDefault: false,
                ProvinceId: 0,
                ProvinceName: "",
                CityId: 0,
                CityName: "",
                AreaId: 0,
                AreaName: "",
                StreetId: 0,
                StreetName: "",
                ZipCode: null,
            },
            selectCity: "",
            currentCity: "",
        }
    },
    mounted() {
        this.getListHeight()
    },
    methods: {
        getListHeight() {
            //获取商品高度
            getElementHeight(this, ".main", (e) => {
                this.listHeight = e + "px"
                this.loadOk = true
                //#ifdef H5
                setTimeout((e) => {
                    this.refresh()
                }, 100)
                //#endif
                this.getLocation()
            })
        },
        searchFn(e) {
            this.param.searchKey = e
            this.refresh()
        },
        getData(page) {
            this.param.pageSize = page.size
            this.param.pageIndex = page.num
            GePurchaseBusinessList(this.param).then((res) => {
                if (res.State == 1) {
                    this.mescroll.endBySize(res.Data.length, res.RecordAmout)
                    this.shopDatas = res.Data
                } else {
                    this.mescroll.endBySize(0, 0)
                }
            })
        },
        //刷新
        refresh() {
            this.downRefresh()
        },
        //刷新
        downRefresh() {
            this.shopDatas = []
            this.mescroll.resetUpScroll()
        },
        selectFn(item) {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit("getSelectData", {
                ...item,
            })
            this.easyGoBack()
        },
        //获取当前定位地址
		//#ifdef APP
		getLocation() {
		   return new Promise(async (resolve) => {
			   let authFlag = await authorizeUtils.showAuthTipModal("android.permission.ACCESS_FINE_LOCATION")
			   if (!authFlag){
					authorizeUtils.showManualAuth("android.permission.ACCESS_FINE_LOCATION")
					setTimeout((e) => {
					    this.refresh()
					}, 100)
					return resolve(null)
			   }
				let that = this
				getLocation(
					(res) => {
						if (res) {
							that.currentCity = res.address.city
							that.selectCity = res.address.city
							that.param.address = res.address.city
				
							that.getP(res.address)
							that.$forceUpdate()
						}
					},
					(error) => {
						that.currentCity = "定位失败"
						setTimeout((e) => {
						    that.refresh()
						}, 100)
					},
				)
		
		   })
		},
		//#endif
		// #ifndef APP
        getLocation() {
            getLocation(
                (res) => {
                    if (res) {
                        this.currentCity = res.address.city
                        this.selectCity = res.address.city
                        this.param.address = res.address.city

                        this.getP(res.address)
                        this.$forceUpdate()
                    }
                },
                (error) => {
                    this.currentCity = "定位失败"
                },
            )
        },
		//#endif
        getP(res) {
            let item = {
                ContactAddress: res.street + res.streetNum,
                ProvinceName: res.province,
                CityName: res.city,
                AreaName: res.district,
                StreeName: res.street,
                ContactName: "111",
                ContactTel: "13400000000",
            }
            let list = []
            list.push(item)
            let param = {
                list: list,
            }
            this.batchSmartParse(param)
        },
        //智能识别
        batchSmartParse(param) {
            batchSmartParse(param).then((res) => {
                if (res.Data && res.Data.list && res.Data.list.length > 0) {
                    let result = res.Data.list[0]
                    this.location = result
                    this.param.cityId = result.CityId
                    this.$forceUpdate()
                    this.refresh()
                }
            })
        },
        upFn(data) {
            this.param = data
            this.selectCity = data.address
            this.$forceUpdate()
            this.refresh()
        },
    },
}
</script>

<style lang="scss">
.search-bar-box {
    background: $color-white;
    padding: $app-page-padding;
}
.current-lbs {
    background: $color-white;
    padding: 0 $app-page-padding $app-page-padding;
    display: flex;
    flex-direction: column;
    margin-bottom: 20rpx;
    .title {
        font-size: $font-size-28;
        color: $app-color-text-tip;
    }
    .lbs-name {
        font-weight: bold;
        margin-top: 10rpx;
        font-size: $font-size-32;
    }
}
</style>
